// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.asm;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class OpcodeUtils {
  public static int getLoadOpcode(Type type) {
    switch (type.getSort()) {
      case Type.BOOLEAN:
      case Type.BYTE:
      case Type.SHORT:
      case Type.CHAR:
      case Type.INT:
        return Opcodes.ILOAD;
      case Type.LONG:
        return Opcodes.LLOAD;
      case Type.FLOAT:
        return Opcodes.FLOAD;
      case Type.DOUBLE:
        return Opcodes.DLOAD;
      case Type.OBJECT:
        return Opcodes.ALOAD;
      case Type.ARRAY:
        return Opcodes.AALOAD;
      default:
        throw new IllegalArgumentException("Illegal type");
    }
  }

  public static int getStoreOpcode(Type type) {
    switch (type.getSort()) {
      case Type.BOOLEAN:
      case Type.BYTE:
      case Type.SHORT:
      case Type.CHAR:
      case Type.INT:
        return Opcodes.ISTORE;
      case Type.LONG:
        return Opcodes.LSTORE;
      case Type.FLOAT:
        return Opcodes.FSTORE;
      case Type.DOUBLE:
        return Opcodes.DSTORE;
      case Type.OBJECT:
        return Opcodes.ASTORE;
      case Type.ARRAY:
        return Opcodes.AASTORE;
      default:
        throw new IllegalArgumentException("Illegal type");
    }
  }
  
  public static boolean isStore(int opcode) {
    return
        opcode == Opcodes.ASTORE ||
        opcode == Opcodes.ISTORE ||
        opcode == Opcodes.LSTORE ||
        opcode == Opcodes.FSTORE ||
        opcode == Opcodes.DSTORE;
  }

  public static boolean isLoad(int opcode) {
    return
        opcode == Opcodes.ALOAD ||
        opcode == Opcodes.ILOAD ||
        opcode == Opcodes.LLOAD ||
        opcode == Opcodes.FLOAD ||
        opcode == Opcodes.DLOAD;
  }
}
